﻿using System.ComponentModel;
using DevExpress.Xpf.Map;
using Loki.Commands;
using Loki.UI;
using Loki.Utils;
using Luna.Model.Crm;

namespace Luna.UI.Crm
{
    public class EntityEditViewModel : Screen
    {
        public ICrmService Crm { get; set; }

        #region Entity

        private static PropertyChangedEventArgs argsEntityChanged = ObservableHelper.CreateChangedArgs<EntityEditViewModel>(x => x.Entity);

        private static PropertyChangingEventArgs argsEntityChanging = ObservableHelper.CreateChangingArgs<EntityEditViewModel>(x => x.Entity);

        private IEditableEntity entity;

        public IEditableEntity Entity
        {
            get
            {
                return entity;
            }

            set
            {
                if (value != entity)
                {
                    NotifyChanging(argsEntityChanging);
                    if (entity != null)
                    {
                        Toolkit.Events.Changed.UnregisterSource(entity);
                    }

                    entity = value;
                    NotifyChangedAndDirty(argsEntityChanged);
                }
            }
        }

        #endregion Entity

        #region CenterPoint

        private static PropertyChangedEventArgs argsCenterPointChanged = ObservableHelper.CreateChangedArgs<GeoSearchViewModel>(x => x.CenterPoint);

        private static PropertyChangingEventArgs argsCenterPointChanging = ObservableHelper.CreateChangingArgs<GeoSearchViewModel>(x => x.CenterPoint);

        private GeoPoint centerPoint;

        public GeoPoint CenterPoint
        {
            get
            {
                return centerPoint;
            }

            set
            {
                if (value != centerPoint)
                {
                    NotifyChanging(argsCenterPointChanging);
                    centerPoint = value;
                    NotifyChanged(argsCenterPointChanged);
                }
            }
        }

        #endregion CenterPoint

        public EntityEditViewModel()
        {
            DisplayName = "Chargement...";
        }

        protected override void OnInitialize()
        {
            Commands.Handle(ApplicationCommands.Save, Application_Save_CanExecute, Application_Save_Execute);
            this.WatchPropertyChanged(this, x => x.Entity, v => v.Entity_Changed);
            this.WatchPropertyChanged(Entity, x => x.FullName, v => v.Entity_FullNameChanged);
            base.OnInitialize();
        }

        protected override void OnLoad()
        {
            base.OnLoad();

            PrepareData();
        }

        private void Application_Save_CanExecute(object sender, CanExecuteCommandEventArgs e)
        {
            e.CanExecute |= Entity != null && Entity.IsChanged;
        }

        private void Application_Save_Execute(object sender, CommandEventArgs e)
        {
            try
            {
                Crm.Save(Entity);
                PrepareData();
            }
            catch (LokiException ex)
            {
            }
        }

        private void PrepareData()
        {
            if (Entity != null)
            {
                State = Entity;
                DisplayName = Entity.FullName;

                var localisation = Crm.GetLocalization(Entity);
                if (localisation != null)
                {
                    CenterPoint = new GeoPoint(localisation.Latitude, localisation.Longitude);
                }

                Entity.AcceptChanges();
            }
        }

        private void Entity_Changed(object sender, PropertyChangedEventArgs e)
        {
            this.WatchPropertyChanged(Entity, x => x.Name, v => v.Entity_FullNameChanged);
            PrepareData();
        }

        private void Entity_FullNameChanged(object sender, PropertyChangedEventArgs e)
        {
            DisplayName = Entity.FullName;
        }
    }
}